package com.gzsxy.esjy.service.auth.shiro.config;

import com.gzsxy.esjy.service.base.dto.response.UserRsp;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;


/**
 * @author xiaolong
 * @version 1.0
 * @description: 获取当前登录信息
 * @date 2022/2/7 17:14
 */
@Slf4j
public class CurrentUserUtil {

    public static Subject getSubject(){
        Subject subject = SecurityUtils.getSubject();
        if (null == subject){
            //未登录
            log.warn("未登录");
        }
        return subject;
    }

    /**
     * @description: 获取登录用户信息
     * @param
     * @return
     */
    public static UserRsp getUser(){
        PrincipalCollection principals = getSubject().getPrincipals();
        if (null == principals){
            return null;
        }
        return (UserRsp) principals.getPrimaryPrincipal();
    }


    public static Session getSession(){
        return getSubject().getSession();
    }
}
